/*
 * StringToInteger.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.text;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.InputPort;

/**
 * StringToInteger - parse a String as an Integer
 * 
 * @see Integer#parseInt(String)
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class StringToInteger extends AbstractNode {

	/**
	 * IN_STRING
	 */
	private static final String IN_STRING = "STRING";

	/**
	 * OUT_INTEGER
	 */
	private static final String OUT_INTEGER = "INTEGER";

	/**
	 * CONFIG_RADIX
	 */
	private static final String CONFIG_RADIX = null;

	/**
	 * DEFAULT_RADIX
	 */
	private static final Integer DEFAULT_RADIX = new Integer(10);

	/**
	 * radix
	 */
	private InputPort radix;

	/**
	 * StringToInteger Constructor
	 * 
	 */
	public StringToInteger() {
		this("S2I" + nr++, false);
	}

	/**
	 * StringToInteger Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public StringToInteger(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		int r = ((Integer) radix.fetchData()).intValue();
		Integer i = Integer.parseInt((String) getInputPort().fetchData(), r);
		getOutputPort().pushData(new Integer(i));
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		addInputPort(IN_STRING, String.class, true);
		addOutputPort(OUT_INTEGER, Integer.class);
		radix = addConfigurationPort(CONFIG_RADIX, Integer.class, DEFAULT_RADIX);
	}

}
